package com.qianfeng.openplatform.sercice.impl;

import com.qianfeng.openplatform.sercice.CacheService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class CacheServiceImpl implements CacheService {
    @Autowired
//@Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public boolean cacheStrSet(String key, String value, Long l) throws Exception {
        redisTemplate.opsForValue().set(key,value);
        if (l!=null){
                redisTemplate.expire(key,l,TimeUnit.MILLISECONDS);
        }
        return true;
    }

    @Override
    public boolean save2Redis(String key, String value, Long expireTime) throws Exception {
        redisTemplate.opsForValue().set(key,value);
        if (expireTime != -1){
            redisTemplate.expire(key,expireTime,TimeUnit.MILLISECONDS);
        }
        return true;
    }

    @Override
    public String getFromRedis(String key) throws Exception {

        return (String)redisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean deleteKey(String key) throws Exception {

        return redisTemplate.delete(key);
    }

    @Override
    public boolean expire(String key, long expireTime) throws Exception {
        return redisTemplate.expire(key,expireTime,TimeUnit.MILLISECONDS);
    }

    @Override
    public Long getAutoIncrementId(String key) throws Exception {
        return redisTemplate.opsForValue().increment(key);
    }

    @Override
    public Set<Object> sMembers(String key) throws Exception {
        Set<Object> members = redisTemplate.opsForSet().members(key);
        return members;
    }

    @Override
    public Long sAdd(String key, String value, Long expireTime) throws Exception {
        Long add = redisTemplate.opsForSet().add(key, value);
        if(expireTime!= -1){
            redisTemplate.expire(key,expireTime,TimeUnit.MILLISECONDS);
        }
        return add;
    }

    @Override
    public Long sAdd(String key, String[] value, Long expireTime) throws Exception {
        Long add = redisTemplate.opsForSet().add(key, value);
        if (expireTime>0){
            redisTemplate.expire(key,expireTime,TimeUnit.MILLISECONDS);
        }
        return add;
    }

    @Override
    public Long sRemove(String key, String value) throws Exception {
        return redisTemplate.opsForSet().remove(key,value);
    }

    @Override
    public void hSet(String key, String field, String value) throws Exception {
        redisTemplate.opsForHash().put(key,field,value);
    }

    @Override
    public String hGet(String key, String field) throws Exception {
        Object o = redisTemplate.opsForHash().get(key, field);

        return o.toString();
//        return o==null? null:o.toString();
//        return (String) redisTemplate.opsForHash().get(key,field);
    }

    @Override
    public Map<Object, Object> hGetAll(String key) throws Exception {
        return redisTemplate.opsForHash().entries(key);
    }

    @Override
    public boolean setNX(String key, String value, long expireSecond) throws Exception {
     return    redisTemplate.opsForValue().setIfAbsent(key,value,expireSecond,TimeUnit.MILLISECONDS);
    }

    @Override
    public boolean hMset(String key, Map<Object, Object> map) throws Exception {
        redisTemplate.opsForHash().putAll(key,map);
        return true;
    }

    @Override
    public Set<String> findKeyByPartten(String partten) throws Exception {
        return redisTemplate.keys(partten);
    }

    @Override
    public Long hIncrement(String key, String field, long delta) throws Exception {
        return redisTemplate.opsForHash().increment(key,field,delta);
    }
}
